<?php
class FastDatabase{
	// ===================================================>
	
    // -- to make instalation more convinient, now this data shold be geted via PasswordSeeker class
    // -- at the  connect_to_the_db
    private static $server="";
	private static $username="";
	private static $password="";
	private static $database="";
    // --
	
	private static $table_calls="log_deb_calls"; // table contain data about all debug information in this call
	private static $table_items="log_deb_items"; // table wich contain information about one debug call (array, object, sql)
	private static $table_system="log_deb_system"; // Here I store important system serealized arrays
	
	private static  $color_caller_id=1; // Id of the serialised array with colors for callers from db
	
	 public static $log_colors=array("D4FF8A","62D04C","A1FD00","7CC202","68FF6F","FFFAAF","FCF361","FCED03","C3DAFF","74A8FE","9A9AFF");
    public static $log_color_count=0; // Index of current color I'll use
    public static $log_arr_matched_callers=false; // assocc class name - color; this array contain name on caller as kay, and color I've add to it
	
	
	
	protected static $db_res=false; // Current recourse ret by my sql connection
	
	private static $current_call_id=false; // The id of current call connection
	private static $current_caller_name=""; // The name of the class wich call this debug write to log
	//==================================================>
	
	
	/**
	 * This function calling every time, do not metter need you it or dose not need
	 * if you do not need this call (tha array from db already getted this will simply return)
	 * Enter description here ...
	 */
	private static function get_matched_colors(){
		if(false!==self::$log_arr_matched_callers) return; // the array already getted
		
		// Select serialased array from db
		
		$sql="SELECT data FROM ".self::$table_system." WHERE id=".self::$color_caller_id.";";
    	$ans=mysql_query($sql,self::$db_res);
    	if(false==$ans){die(mysql_error(self::$db_res));}
    	
    	$row = mysql_fetch_assoc($ans);
    	
    	if(false==$ans){die("Some how cannot get colors for my coloring calls");}
    	
    	self::$log_arr_matched_callers=unserialize($row['data']);
	}
	
	
	/**
	 * Save colors to the database
	 * Enter description here ...
	 */
	private static function save_matched_colors(){
		if(false!==self::$log_arr_matched_callers) return; // the array already getted
		
		// Select serialased array from db
		$the_data=serialize(self::$log_arr_matched_callers);
		$sql="UPDATE ".self::$table_system." SET data='".$the_data."' WHERE id=".self::$color_caller_id.";";
    	$ans=mysql_query($sql,self::$db_res);
    	if(false==$ans){die(mysql_error(self::$db_res));}
	}
    
    
    /**
     * Connect to the bd, and create new row in caller table]
     * now to making instalation of DEBUG_FAST more easy,
     * mysql connection data will be getted wive PasswordSeeker object
     */
    public static function connect_to_the_db(){
        
        // Getting Mysql connection data
        // Class to grab this dat from ini file
        require('PasswordSeeker.php');
        $PasswordSeekerObject=new PasswordSeeker("../_local/config.ini.php");
        self::$server = $PasswordSeekerObject->server;
        self::$username = $PasswordSeekerObject->username;
        self::$password = $PasswordSeekerObject->password;
        self::$database = $PasswordSeekerObject->database;        
    	// connect to the db
    	self::$db_res=mysql_connect(self::$server,self::$username,self::$password);
    	if(false===self::$db_res){ die("cannot connect to database for print debug information");}
    	
    	// select database
    	$ans=mysql_select_db(self::$database,self::$db_res);
    	if(false==$ans){die(mysql_error(self::$db_res));}
    	
    	return true;
    }
    
    /**
     * 
     * Return empty array or one row from $table_items
     */
    public static function get_item($id){
    	$id=(int)$id;
    	$sql="SELECT * FROM ".self::$table_items." WHERE id=$id";
    	$ans=mysql_query($sql,self::$db_res);
    	if(false==$ans){die(mysql_error(self::$db_res));}
    	 while ($row = mysql_fetch_assoc($ans)) {
    		return $row;
    	}
    	return array();    	
    }
    
    /**
     * 
     * get Htlm table with current calls
     * @param unknown_type $id
     */
    public static function get_call($id){
    	self::get_matched_colors();
    	$flag_color_arr_changed=false; // If color arr changed we need to put it in to the database
    	
    	$id=(int)$id;
    	$html="<table border=1>";
    	
    	$sql="SELECT id, small_descr, type,time_,caller FROM ".self::$table_items." WHERE pid=$id";
    	$ans=mysql_query($sql,self::$db_res);
    	if(false==$ans){die(mysql_error(self::$db_res));}
    	$flag=0;
    	 while ($row = mysql_fetch_assoc($ans)) {
    	 	// Set header
    	 	if(0==$flag){
    	 		$flag=1;
    	 		$html.="<tr style='background:#4BD239;font-weight:bold;color:white'>";
    	 		 foreach ($row as $name=>$value){
    	 		 	$html.="<th><b>$name</b></th>";
    	 		 }
    	 		$html.="</tr>";
    	 	}
    	 	
    	 	// Selecting color
    	 	//--------------------
    	 	$color_standart="A1FD00";
    	 	 	$match=explode(":", $row['caller']);
    	 	$match=$match[0]==""?"empty11":$match[0];
    	 	// No such match in array
                    if(!array_key_exists($match, self::$log_arr_matched_callers))
                    {
                    	$c_i=(++self::$log_color_count)%count(self::$log_colors);
    					$color_standart=self::$log_colors[$c_i];
    					self::$log_arr_matched_callers[$match]=$color_standart;
    					$flag_color_arr_changed=true;
        			}
        			// Have such match get color from array
        			else{
        				$color_standart=self::$log_arr_matched_callers[$match];
        			}
    	 	//--------------------
    	 	$html.="<tr style=\"background-color: #$color_standart;\">";
    	 	$the_id=0;
    	 		 foreach ($row as $name=>$value){
    	 		 	if('id'==$name){
    	 		 		$html.="<td><input type=\"checkbox\"></td><td><a href=\"/debug_fast/get_debug.php?id=$value\">$value</a></td>";
    	 		 	}else{
    	 		 		$html.="<td>$value</td>";
    	 		 	}
    	 		 }
    	 		$html.="</tr>";
    	}
    	
    	$html.="</table>";
    	if($flag_color_arr_changed) self::save_matched_colors();
    	return $html;
    }
    public static function get_all_call($id=0){
    	self::get_matched_colors();
    	$flag_color_arr_changed=false; // If color arr changed we need to put it in to the database
    	
    	$id=(int)$id;
    	$html="<table border=1>";
    	
    	$sql="SELECT * FROM ".self::$table_calls.";";
    	$ans=mysql_query($sql,self::$db_res);
    	if(false==$ans){die(mysql_error(self::$db_res));}
    	$flag=0;
    	 while ($row = mysql_fetch_assoc($ans)) {
    	 	// Set header
    	 	if(0==$flag){
    	 		$flag=1;
    	 		$html.="<tr style='background:#4BD239;font-weight:bold;color:white'>";
    	 		 foreach ($row as $name=>$value){
    	 		 	$html.="<td><b>$name</b></td>";
    	 		 }
    	 		$html.="</tr>";
    	 	}
    	 	
    	 	// Selecting color
    	 	//--------------------
    	 	$color_standart="A1FD00";
    	 	$match=explode(":", $row['caller']);
    	 	$match=$match[0]==""?"empty11":$match[0];
    	 	// No such match in array
                    if(!array_key_exists($match, self::$log_arr_matched_callers))
                    {
                    	$c_i=(++self::$log_color_count)%count(self::$log_colors);
    					$color_standart=self::$log_colors[$c_i];
    					self::$log_arr_matched_callers[$match]=$color_standart;
    					$flag_color_arr_changed=true;
        			}
        			// Have such match get color from array
        			else{
        				$color_standart=self::$log_arr_matched_callers[$match];
        			}
    	 	//--------------------
    	 	$html.="<tr style=\"background-color: #$color_standart;\">";
    	 	$the_id=0;
    	 		 foreach ($row as $name=>$value){
    	 		 	if('id'==$name){
    	 		 		$html.="<td><a href=\"/debug_fast/get_call.php?id=$value\">$value</a></td>";
    	 		 	}else{
    	 		 		$html.="<td>$value</td>";
    	 		 	}
    	 		 }
    	 		$html.="</tr>";
    	}
    	
    	$html.="</table>";
    	if($flag_color_arr_changed) self::save_matched_colors();
    	
    	return $html;
    }
    
}